package com.fingard.dsp.bank.directbank.cmb02;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IRealTime;
import com.fingard.dsp.bank.directbank.cmb02.util.SignUtil;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;

import java.io.File;
import java.util.Date;

public class CMB02RealTime extends CMB02Base implements IRealTime {
    @Override
    public RetAtsSingle d_9108_RealTimeRec(ReqSingle p_req9108) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req9108.reqHeader);
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(p_req9108);

        try {
            String tmpMacKey = "";
            String tmpMacKeyPath = getCfgDirectory() + File.separator + "macKey";
            if (!new File(tmpMacKeyPath).exists()) {
                String[] tmpMacKeyRet = exchangeMacKey();
                if (tmpMacKeyRet[0].length() != 0) {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpMacKeyRet[1];
                } else {
                    tmpMacKey = FileHelper.readFileText(tmpMacKeyPath, getCharset());
                }
            } else {
                tmpMacKey = FileHelper.readFileText(tmpMacKeyPath, getCharset());
            }
            WriteBankLogLn("macKey:" + tmpMacKey);

            StringBuilder tmpEachRecordReq = new StringBuilder();

            StringBuilder tmpEachRecordBody = new StringBuilder();
            tmpEachRecordBody.append(String.format("%-20s", p_req9108.singleDetail.payAct));
            tmpEachRecordBody.append(String.format("%-20s", bankFront.corpNo));
            tmpEachRecordBody.append(String.format("%-20s", bankFront.org));
            tmpEachRecordBody.append(String.format("%-4s", ""));
            tmpEachRecordBody.append("RMB");
            tmpEachRecordBody.append(String.format("%20s", StringHelper.amountMultiply100(p_req9108.singleDetail.payAmount).replace(" ", "0")));
            tmpEachRecordBody.append(String.format("%-60s", p_req9108.singleDetail.payName));
            tmpEachRecordBody.append(String.format("%-20s", p_req9108.singleDetail.cellPhone));
            tmpEachRecordBody.append(String.format("%-3s", convertCertTypeToBank(p_req9108.singleDetail.certType)));
            tmpEachRecordBody.append(String.format("%-30s", p_req9108.singleDetail.certNum));
            tmpEachRecordBody.append(String.format("%-20s", ""));
            tmpEachRecordBody.append(String.format("%-20s", ""));
            tmpEachRecordBody.append(String.format("%-120s", ""));
            tmpEachRecordBody.append(String.format("%-32s", getBankConfig().getValueAsString("paymentBasis")));
            tmpEachRecordBody.append(String.format("%-48s", ""));
            tmpEachRecordBody.append(String.format("%-80s", ""));

            StringBuilder tmpEachRecordHeader = new StringBuilder();
            tmpEachRecordHeader.append("CMBA");
            tmpEachRecordHeader.append(String.format("%-4s", tmpEachRecordBody.toString().getBytes(getCharset()).length));
            tmpEachRecordHeader.append("CMSK");
            tmpEachRecordHeader.append(String.format("%-8s", ""));
            tmpEachRecordHeader.append(String.format("%-6s", ""));
            tmpEachRecordHeader.append(String.format("%-30s", ""));
            Date tmpDateNow = new Date();
            tmpEachRecordHeader.append(Format.Date8Format.format(tmpDateNow));
            tmpEachRecordHeader.append(Format.Time6Format.format(tmpDateNow));
            tmpEachRecordHeader.append(String.format("%-25s", p_req9108.singleDetail.askNumber));
            tmpEachRecordHeader.append("0.2.0");
            tmpEachRecordHeader.append(String.format("%-10s", bankFront.corpNo));
            tmpEachRecordHeader.append(String.format("%-16s", SignUtil.ansi99(tmpMacKey, tmpEachRecordBody.toString())));

            tmpEachRecordReq.append(tmpEachRecordHeader).append(tmpEachRecordBody);

            String[] tmpStrRet = sendToBank(tmpEachRecordReq.toString());
            if (tmpStrRet[0].length() == 0) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = tmpStrRet[1].substring(52).trim();
                tmpRetDetail.transState = StringHelper.joinNotEmpty("+", tmpStrRet[1].substring(0, 7), tmpStrRet[1].substring(45, 52).trim());
                tmpRetDetail.payInfoCode = tmpStrRet[1].substring(45, 52).trim();
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.toString();
            WriteBankLogLn(ex);
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsSingle d_9144_QryRealTimeRec(ReqQrySingle p_req9144) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req9144.reqHeader);
        RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(p_req9144);

        try {
            String tmpMacKey = "";
            String tmpMacKeyPath = getCfgDirectory() + File.separator + "macKey";
            if (!new File(tmpMacKeyPath).exists()) {
                String[] tmpMacKeyRet = exchangeMacKey();
                if (tmpMacKeyRet[0].length() != 0) {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpMacKeyRet[1];
                } else {
                    tmpMacKey = FileHelper.readFileText(tmpMacKeyPath, getCharset());
                }
            } else {
                tmpMacKey = FileHelper.readFileText(tmpMacKeyPath, getCharset());
            }
            WriteBankLogLn("macKey:" + tmpMacKey);

            StringBuilder tmpEachRecordReq = new StringBuilder();

            StringBuilder tmpEachRecordBody = new StringBuilder();
            tmpEachRecordBody.append(String.format("%-20s", ""));
            tmpEachRecordBody.append(String.format("%-20s", bankFront.corpNo));
            tmpEachRecordBody.append(String.format("%-4s", ""));
            Date tmpBgnDate = p_req9144.getBgnDateTime();
            tmpEachRecordBody.append(Format.Date8Format.format(tmpBgnDate));
            tmpEachRecordBody.append(String.format("%-30s", p_req9144.askNumber));

            StringBuilder tmpEachRecordHeader = new StringBuilder();
            tmpEachRecordHeader.append("CMBA");
            tmpEachRecordHeader.append(String.format("%-4s", tmpEachRecordBody.toString().getBytes(getCharset()).length));
            tmpEachRecordHeader.append("CMDB");
            tmpEachRecordHeader.append(String.format("%-8s", ""));
            tmpEachRecordHeader.append(String.format("%-6s", ""));
            tmpEachRecordHeader.append(String.format("%-30s", ""));

            tmpEachRecordHeader.append(Format.Date8Format.format(tmpBgnDate));
            tmpEachRecordHeader.append(Format.Time6Format.format(tmpBgnDate));
            tmpEachRecordHeader.append(String.format("%-25s", getQryTransID()));
            tmpEachRecordHeader.append("0.2.0");
            tmpEachRecordHeader.append(String.format("%-10s", bankFront.corpNo));
            tmpEachRecordHeader.append(String.format("%-16s", SignUtil.ansi99(tmpMacKey, tmpEachRecordBody.toString())));

            tmpEachRecordReq.append(tmpEachRecordHeader).append(tmpEachRecordBody);

            String[] tmpStrRet = sendToBank(tmpEachRecordReq.toString());
            if (tmpStrRet[0].length() == 0) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = tmpStrRet[1].substring(52).trim();
                String tmpRespCode = tmpStrRet[1].substring(0, 7);
                if ("CMBMB99".equals(tmpRespCode)) {
                    tmpRetDetail.transState = StringHelper.joinNotEmpty("+", tmpRespCode, tmpStrRet[1].substring(45, 52).trim());
                    tmpRetDetail.payInfoCode = tmpStrRet[1].substring(45, 52).trim();
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpRespCode + "+" + tmpStrRet[1].substring(52).trim();
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }
}